# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message: 获取长安网站备件信息


import pymongo
import json
import requests
import os
import hashlib
from collections import OrderedDict
from untils.tools import quote_keys_for_json
import time

class PartBaseInfo:

    def __init__(self, **kwargs):
        self.base_path = os.getcwd() + '/../{}_html/'.format("changan")  # 获取当前路径并拼接出存储html文件的路径
        if not os.path.exists(self.base_path):  # 如果路径不存在,就创建一个
            os.mkdir(self.base_path)
        self.base_imgPath = os.getcwd() + '/../changan_img/'
        if not os.path.exists(self.base_imgPath):
            os.mkdir(self.base_imgPath)

        self.mongoClient = pymongo.MongoClient(host='192.168.10.222', port=27017)
        self.mongoDb = self.mongoClient['changan']
        self.mongoCol = self.mongoDb['PartBaseInfo']

        # 解析用数据
        self.keys = ["PART_OLDCODE",  # 备件编码
                     "PART_CNAME",  # 备件名称
                     "PART_CODE",  # 备件件号
                     "SALE_PRICE",  # 调拨价
                     "RETAIL_PRICE",  # 零售价
                     ]

        # 请求所需数据
        self.cookies = {
            "pmsserver7": "A1410FA32FE0719D3BD1BC033E37AA7B",
            "login_acnt": "JS00008A",
            "_pk_ses.11.0e90": "*",
            "_pk_id.11.0e90": "2a0f423c89ec4be8.1547451445.6.1547518780.1547518765.",
            # "": "",
            # "": "",
        }
        self.headers = {
            "Host": "pms.changan.com.cn",
            "X-Prototype-Version": "1.6.0",
            "Origin": "http://pms.changan.com.cn",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
            "Referer": "http://pms.changan.com.cn/PMS/parts/baseManager/partBaseQuery/partMainDataQuery/partMainDataQuery/partBaseQueryInit.do",
            # "": "",
            # "": "",
        }
        self.postData = OrderedDict(
            orgId="2000000000081408",
            PART_OLDCODE="",
            PART_CNAME="",
            PART_CODE="",
            PRODUCE_FAC="",
            LACK_STATE="",
            IN_PLAN="",
            PART_SORT="",
            SELLPART_PROPERTY="",
            FRAME_ID="",
            SALE_PRICE="",
            LC="",
            CAR_TYPE="",
            IS_RECOVER="",
            REMARK="",
            STATE="10011001",
            ASSEMBLY_LEVEL="",
            PRODUCE_STATE="",
            buyerId="",
            ARTICLES_TYPE="",
            IS_OIL="",
            IS_SPECIAL="",
            IS_TIME="",
            orderCol="",
            order="",
            _="",
        )

        self.base_url = 'http://pms.changan.com.cn/PMS/parts/baseManager/partBaseQuery/partMainDataQuery/partMainDataQuery/queryPartBaseInfo.json?curPage={}&json=%7B%7D'
        self.imgUrl = 'http://pms.changan.com.cn/PMS/parts/image/PartImgManager/getImgsByPartIdSx.json'
        self.base = 'http://pms.changan.com.cn'

    def get_md5(self, str):
        '''获取md5加密电子指纹'''
        md5 = hashlib.md5()
        md5.update(str)
        return md5.hexdigest()

    def parse_url(self, url, method, post_data=None, headers=None, tofile=True, **kwargs):
        requests.packages.urllib3.disable_warnings()  # 因为把ssl安全验证关了,所以不让代码报不安全的https请求警告
        pid = kwargs.get("local_pid", "")
        charset = kwargs.get("charset", "utf8")  # 暂时没有用到
        allow_redirects = kwargs.get("allow_redirects", False)  # 是否允许重定向, 默认不重定向,获取原始响应
        if method == 'GET':
            data = "GET" + url + pid  # 拼接data
            md5 = self.get_md5(data)  # 获取此次请求电子指纹
            file_path = self.base_path + md5 + ".html"
            if os.path.exists(file_path):  # 判断文件是否存在,如果存在就从本地读取
                print("GET for local")
                with open(file_path, 'r') as f:
                    res = f.read()
                response = res
            else:  # 如果路径不存在, 从web端请求
                print("GET for web")
                try:
                    response = requests.get(url=url, verify=False, headers=headers, cookies=self.cookies, timeout=30,
                                            allow_redirects=allow_redirects)
                except Exception as e:
                    response = requests.get(url=url, verify=False, headers=headers, cookies=self.cookies, timeout=30,
                                            allow_redirects=allow_redirects)

                # 每次请求都获取cookies, 并更新self.cookies(如果获取到cookies的话)
                cookies = response.cookies.get_dict()
                for k in cookies:
                    self.cookies[k] = cookies[k]

                # 获取location,
                location = response.headers.get("Location", None)
                if location:
                    print("响应头中存在location")
                    self.location = location
                    print(location)
                    # return response.content.decode('utf8')

                if response:
                    if tofile:
                        with open(file_path, 'w') as f:  # 保存此次请求的html
                            f.write(response.content.decode('utf8'))
                    response = response.content.decode("utf8")
                else:
                    response = None

        elif method == "POST":
            data = "POST" + url + json.dumps(post_data)
            md5 = self.get_md5(data.encode())
            file_path = self.base_path + md5 + ".html"
            if os.path.exists(file_path):  # 判断文件是否存在,如果存在就从本地读取
                print("POST for local")
                with open(file_path, 'r') as f:
                    res = f.read()
                response = res
            else:
                print("POST for web")
                try:
                    response = requests.post(url=url, data=post_data, verify=False, headers=headers,
                                             cookies=self.cookies, timeout=30,
                                             allow_redirects=allow_redirects)
                except Exception as e:
                    response = requests.post(url=url, data=post_data, verify=False, headers=headers,
                                             cookies=self.cookies, timeout=30,
                                             allow_redirects=allow_redirects)

                # 获取cookies, 并更新self.cookies
                cookies = response.cookies.get_dict()
                for k in cookies:
                    self.cookies[k] = cookies[k]

                # 获取location,
                location = response.headers.get("Location", None)
                if location:
                    print("响应头中存在location")
                    self.location = location
                    print(location)
                    # response, file_path = self.parse_url(url=location, method="GET", headers=headers, local_pid=pid)
                    # print "已重新请求location地址"
                    # return response, file_path
                    # return response.content.decode('utf8')

                if response:
                    if tofile:
                        with open(file_path, 'w') as f:  # 保存此次请求的html
                            f.write(response.content.decode('utf8'))
                    response = response.content.decode("utf8")

                else:
                    response = None

        return response, file_path

    def get_partImg(self, partCode, partId):
        # 先获取图片url
        imgPost = OrderedDict(
            partId= partId,
            remark= "",
            uploadFile= "",
            _= "",
        )
        try:
            response, filePath = self.parse_url(url=self.imgUrl, method="POST", post_data=imgPost, headers=self.headers)
        except:
            try:
                response, filePath = self.parse_url(url=self.imgUrl, method="POST", post_data=imgPost, headers=self.headers)
            except Exception as e:
                print("图片链接获取失败")
                raise e
        print("图片链接文件: {}".format(filePath))
        try:
            imgInfo_all = json.loads(quote_keys_for_json(response))
        except Exception as e:
            print(e.__repr__())
            return
        imgList = imgInfo_all.get("imgList")
        for imgInfo in imgList:
            fileUrl = self.base + imgInfo.get("FILEURL") if imgInfo.get("FILEURL") else ""
            imgName = fileUrl.split("/")[-1]
            # 判断及创建文件夹
            imgPath = self.base_imgPath + partCode
            if not os.path.exists(imgPath):
                os.mkdir(imgPath)
            imgPath_fina = imgPath + '/' + imgName
            if not os.path.exists(imgPath_fina):
                if fileUrl:
                    cookies = {
                        "_pk_ses.11.0e90": "*",
                        "_pk_id.11.0e90": "2a0f423c89ec4be8.1547451445.6.1547518780.1547518765.",
                    }
                    try:
                        imgData = requests.get(url=fileUrl, headers = self.headers, cookies=cookies, timeout=30, allow_redirects=False)
                    except:
                        try:
                            imgData = requests.get(url=fileUrl, headers = self.headers, cookies=cookies, timeout=30, allow_redirects=False)
                        except Exception as e:
                            print("图片获取失败")
                            continue

                    with open(imgPath_fina, 'wb') as f:
                        f.write(imgData.content)

    def run(self):
        nextPage = 748
        while True:
            url = self.base_url.format(nextPage)
            try:
                response, filePath = self.parse_url(url=url, method="POST", post_data=self.postData, headers=self.headers)
            except:
                response, filePath = self.parse_url(url=url, method="POST", post_data=self.postData, headers=self.headers)
            print("页面文件: {}".format(filePath))
            try:
                partInfo_all = json.loads(quote_keys_for_json(response))
            except Exception as e:
                print(e.__repr__())
                nextPage += 1
                continue
            ps = partInfo_all.get("ps")
            pageSize = ps.get("pageSize")
            partList = ps.get("records")
            totalRecords = ps.get("totalRecords")
            totalPages = ps.get("totalPages")
            curPage = ps.get("curPage")

            # 对零件数据totalRecords进行处理
            for partInfo in partList:
                mainData = {k: partInfo[k] for k in self.keys}
                PART_ID = partInfo.get("PART_ID")  # 用于图片请求
                PART_OLDCODE = mainData.get("PART_OLDCODE")
                self.get_partImg(PART_OLDCODE, PART_ID)
                print(mainData)
                self.mongoCol.insert(mainData)
            nextPage = curPage + 1
            print(nextPage)
            # if nextPage == 414:
            #     break
            if nextPage > totalPages:
                break
            # time.sleep(3)

if __name__ == '__main__':
    tools = PartBaseInfo()
    tools.run()